package com.wander.juc.thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolExecutorExample {
    public static void main(String[] args) {
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(5);
        ThreadPoolExecutor pool = new ThreadPoolExecutor(3, 8, 2, TimeUnit.SECONDS, workQueue);
        for (int i = 0; i < 15; i++) {
            try {
                pool.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + " is running...");
                    try {
                        Thread.sleep(800);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                });
            } catch (Exception e) {
                System.out.println(e.getMessage() + " " + "关闭线程池...");
                pool.shutdown();
            }
        }
    }
}
